package com.travel.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.travel.commons.annotation.SysLog;
import com.travel.commons.shiro.ShiroUserGetter;
import com.travel.commons.utils.BeanUtils;
import com.travel.commons.utils.PageUtils;
import com.travel.commons.utils.R;
import com.travel.commons.utils.SnowNoGenerator;
import com.travel.commons.validator.ValidatorUtils;
import com.travel.commons.validator.groups.AddGroup;
import com.travel.commons.validator.groups.UpdateGroup;
import com.travel.modules.asset.entity.Advertise;
import com.travel.modules.asset.entity.Hotel;
import com.travel.modules.asset.entity.Route;
import com.travel.modules.asset.service.AdvertiseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/ad")
public class SysAdvertiseController {
    @Resource
    private AdvertiseService adService;

    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> param) {
        Integer page = (Integer) param.get("page");
        Integer limit = (Integer) param.get("limit");
        if (page == null || limit == null) {
            return R.error("缺少分页参数");
        }
        List<Advertise> list = adService.getAllAdvertise(param);
        return R.ok().put("page", PageUtils.getPage(list, limit, page));
    }

    @RequestMapping("/list/current")
    public R list2Current(@RequestBody Map<String, Object> param) {
        Integer page = (Integer) param.get("page");
        Integer limit = (Integer) param.get("limit");
        if (page == null || limit == null) {
            return R.error("缺少分页参数");
        }
        param.put("userId", ShiroUserGetter.getUserId());
        List<Advertise> list = adService.getAllAdvertise(param);
        return R.ok().put("page", PageUtils.getPage(list, limit, page));
    }

    @RequestMapping("/info")
    public R info(@RequestBody Advertise form) {
        String adId = form.getAdId();
        if (StringUtils.isBlank(adId)) {
            return R.error("缺少id");
        }
        Advertise ad = adService.getOne(new QueryWrapper<Advertise>().eq("ad_id", adId));
        return R.ok().put("page", ad);
    }

    @SysLog("广告添加")
    @RequestMapping("/add")
    public R add(@RequestBody Advertise form) {
        form.setAdId("ad-" + new SnowNoGenerator().generate());
        form.setUserId(form.getUserId() != null ? form.getUserId() : ShiroUserGetter.getUserId());
        form.setStatus(0);
        ValidatorUtils.validate(form, AddGroup.class);
        adService.save(form);

        return R.ok().put("page", form)
                     .put("id", form.getAdId());
    }

    @SysLog("广告编辑")
    @RequestMapping("/edit")
    public R edit(@RequestBody Advertise form) {
        String adId = form.getAdId();
        Advertise old = adService.getOne(new QueryWrapper<Advertise>().eq("ad_id", adId));
        if (old == null) {
            return R.error("更新目标丢失");
        }
        BeanUtils.copyProperties(form, old);
        ValidatorUtils.validate(old, UpdateGroup.class);
        adService.updateById(old);
        log.warn("用户 {} : 广告更新, 编号 : {}", ShiroUserGetter.getUserId(), adId);
        return R.ok();
    }

    /**
     * 删除指定广告
     * @param param adId - 广告id
     */
    @SysLog("广告删除")
    @RequestMapping("/del")
    public R del(@RequestBody Map<String, String> param) {
        String adId = param.get("adId");
        if (StringUtils.isBlank(adId)) {
            return R.error("缺少id");
        }
        Advertise ad = adService.getOne(new QueryWrapper<Advertise>().eq("ad_id", adId));
        if (ad == null) {
            return R.error("没有该记录");
        }
        adService.removeById(ad.getId());
        log.warn("用户: {}, 酒店删除, 编号: {}", ShiroUserGetter.getUserId(), adId);
        return R.ok();
    }
}
